<?php
/**
 * @author 
 * @copyright 2014
 */
 
class incModelMenu extends CI_Model
{

	function __construct(){
		parent::__construct('menus');
	}
    function getMenu($uri){
        //$current = $this->getMenuUser();
		$current = null;
		$parent = $this->model->table('menus')
							->select('title,link,id,icon')
					        ->where('parent',0)
							->order_by('ordering','ASC')
							->find_all();
		$arr_list = $this->model->table('menus')->select('link,parent')->find_combo('link','parent');
		if(isset($arr_list[$uri])){
			$parentid = $arr_list[$uri];
		}
		else{
			$parentid = -1;
		}
		$menu = '';
		foreach($parent as $item){
			if($parentid == $item->id){
				$class = 'active';
			}
			else{
				$class = '';
			}
			$asub = $this->getChildren($item->id, $current,$uri);
            if($asub){
				$menu.=	'
					<li class="'.$class.' treeview">
						<a href="#">
							<i class="fa '.($item->icon).'"></i>
							<span>'.($item->title).'</span>
							<i class="fa fa-angle-left pull-right"></i>
						</a>';
				$menu.=	'<ul class="treeview-menu">';
				$menu.=$asub;			
				$menu.=	'</ul></li>';
			}
			else{
				$menu.='<li>
					<a href="'.($item->link).'.html">
						<i class="fa '.($item->icon).'"></i><span>'.($item->title).'</span> <small class="badge pull-right bg-green">new</small>
					</a>
				</li>';  
			}
		}
		return $menu;	
    }
    private function getChildren($id, $current,$uri){
		$children = $this->model->table('menus')
							->select('title,link,id,parent,icon')
					        ->where('parent',$id)
							//->where('`id` in ('.$current.')')
							->order_by('ordering','ASC')
							->find_all();
							
        $menu = '';
		foreach($children as $item){
			 //$asub = $this->getChildren($item->id, $current);  
			$menu.='<li><a href="'.($item->link).'.html "><i class="fa '.($item->icon).'"></i>'.($item->title).'</a></li>';	
			 /*if($asub){
				$menu.='<li><a href="#">'.($item->title).'</a>';
				$menu.='<ul>';
				$menu.=$asub;
				$menu.='</ul></li>';  
			 }
			 else{
				$menu.='<li><a href="'.(base_url()).($item->link).'.html"><i class="fa '.($item->icon).'"></i>'.($item->title).'</a></li>'; 
			}*/
		}
		return $menu;
    } 
    function getMenuUser(){
        $groupid = $this->admin->login['groups'];
		print_r($groupid); exit;
        $query = $this->model->table('groups')
							 ->select('params')
							 ->where('id',$groupid)
							 ->find();
							 ;
		$json_parmas = json_decode($query->params);
		if(empty($query->params)){
			//header("Location: ".base_url()."home/logout.html");
			//exit;	
		}
		$str_id = '';
		foreach($json_parmas as $key=>$arr){
			$str_id.= ','.$key;
		}
		return substr($str_id,1);
    }
}